package day1.Exer527;

import day1.Exer.DBUtil;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * :在控制台输入一个学生的全部信息，保存到数据库，
 * 注意日期的处理(PreparedStatement)
 */
public class Exer3 {
    public static void main(String[] args) {
        Scanner in =new Scanner(System.in);
        System.out.println("请输入姓名");
        String sname=in.next();
        System.out.println("请输入性别");
        String sex=in.next();
        System.out.println("请输入电话");
        String telephone=in.next();
        System.out.println("请输入QQ");
        String qq=in.next();
        System.out.println("请输入微信");
        String wechat=in.next();
        System.out.println("请输入班级");
        int gradeid=in.nextInt();
        System.out.println("请输入学日期");
        String enterdate=in.next();
        System.out.println("请输入来自");
        String fromcity=in.next();
        System.out.println("请输入生日");
        String birthday=in.next();
        System.out.println("请输入住址");
        String adress=in.next();
        Connection con=null;
        PreparedStatement pst=null;
        try {
            String sql="insert into s_student(sname,sex,telephone,qq,wechat,gradeid,enterdate," +
                    "fromcity,birthday,address) values (?,?,?,?,?,?,?,?,?,?)";
            con= DBUtil.getConnection();
            pst=con.prepareStatement(sql);
            pst.setString(1,sname);
            pst.setString(2,sex);
            pst.setString(3,telephone);
            pst.setString(4,qq);
            pst.setString(5,wechat);
            pst.setInt(6,gradeid);
            pst.setDate(7, Date.valueOf(enterdate));
            pst.setString(8,fromcity);
            pst.setDate(9,Date.valueOf(birthday));
            pst.setString(10,adress);
            int rs=pst.executeUpdate();
            System.out.println(rs==0?"添加失败":"添加成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            DBUtil.close(pst);
            DBUtil.close(con);
        }
    }
}
